using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;

namespace NTTHAO.CommonWeb
{
    /// <summary>
    /// Summary description for Common
    /// </summary>
    public class Common
    {
        public Common()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        /*
        protected static System.Drawing.Imaging.ImageFormat GetImageFormat(String path)
        {
            try
            {
                switch (Path.GetExtension(path).ToLower())
                {
                    case ".bmp": return System.Drawing.Imaging.ImageFormat.Bmp;
                    case ".jpg": return System.Drawing.Imaging.ImageFormat.Jpeg;
                    case ".jpeg": return System.Drawing.Imaging.ImageFormat.Jpeg;
                    case ".jpe": return System.Drawing.Imaging.ImageFormat.Jpeg;
                    case ".png": return System.Drawing.Imaging.ImageFormat.Png;
                    default: break;
                }
            }
            catch (Exception)
            {
            }
            return System.Drawing.Imaging.ImageFormat.Jpeg;
        }

        public static System.Drawing.Image ResizeImage(System.Drawing.Image imageSource, int maxWidth, int maxHeight)
        {
            int resizedWidth = maxWidth;
            int resizedHeight = maxHeight;

            resizedWidth = maxWidth;
            resizedHeight = Convert.ToInt32(imageSource.Height * ((resizedWidth * 1.0) / (imageSource.Width * 1.0)));

            if (resizedHeight > maxHeight)
            {
                resizedHeight = maxHeight;
                resizedWidth = Convert.ToInt32((imageSource.Width * 1.0) * ((resizedHeight * 1.0) / (imageSource.Height * 1.0)));
            }

            System.Drawing.Image oThumbNail = new System.Drawing.Bitmap(imageSource, resizedWidth, resizedHeight);
            System.Drawing.Graphics oGraphic = System.Drawing.Graphics.FromImage(oThumbNail);

            oGraphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            oGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            oGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            System.Drawing.Rectangle oRectangle = new System.Drawing.Rectangle(0, 0, resizedWidth, resizedHeight);
            oGraphic.DrawImage(imageSource, oRectangle);
            return oThumbNail;
        }

        public static void ResizeImage(string imageFileName, int maxWidth, int maxHeight)
        {
            try
            {
                if (System.IO.File.Exists(imageFileName))
                {
                    string fileExt = System.IO.Path.GetExtension(imageFileName);

                    if (Constant.RESIZABLE_IMAGE_FILE_EXTENTIONS.Contains(fileExt.ToLower()))
                    {
                        System.Drawing.Image img = null;
                        System.Drawing.Image imgFixed = null;
                        try
                        {
                            img = System.Drawing.Image.FromFile(imageFileName, true);
                            if (img.Width > maxWidth || img.Height > maxHeight)
                            {
                                imgFixed = NTTHAO.CommonWeb.Common.FixMaxSizeImage(img, maxWidth, maxHeight);
                                img.Dispose();
                                img = null;
                                / *
                                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                                ImageCodecInfo ici = null;
                                foreach (ImageCodecInfo codec in codecs)
                                {
                                    if (codec.MimeType.Equals("image/jpeg", StringComparison.OrdinalIgnoreCase))
                                    {
                                        ici = codec;
                                        break;
                                    }
                                }
                                EncoderParameters ep = new EncoderParameters();
                                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 90); 
                                * /
                                imgFixed.Save(imageFileName, Common.GetImageFormat(imageFileName));// System.Drawing.Imaging.ImageFormat.Jpeg);
                                //imgFixed.Save(imageFileName, ici, ep);
                            }
                        }
                        catch (Exception) { }
                        finally
                        {
                            if (img != null)
                            {
                                img.Dispose();
                                img = null;
                            }
                            if (imgFixed != null)
                            {
                                imgFixed.Dispose();
                                imgFixed = null;
                            }
                        }
                    }
                }
            }
            catch (Exception) { }
        }

        public static System.Drawing.Image FixMaxSizeImage(System.Drawing.Image imageSource, int maxWidth, int maxHeight)
        {
            int resizedWidth = imageSource.Width;
            int resizedHeight = imageSource.Height;

            if (resizedWidth > maxWidth)
            {
                resizedWidth = maxWidth;
                resizedHeight = Convert.ToInt32(imageSource.Height * ((resizedWidth * 1.0) / (imageSource.Width * 1.0)));
            }

            if (resizedHeight > maxHeight)
            {
                resizedHeight = maxHeight;
                resizedWidth = Convert.ToInt32((imageSource.Width * 1.0) * ((resizedHeight * 1.0) / (imageSource.Height * 1.0)));
            }

            System.Drawing.Bitmap oThumbNail = new System.Drawing.Bitmap(resizedWidth, resizedHeight, imageSource.PixelFormat);
            oThumbNail.SetResolution(73, 73);
            System.Drawing.Graphics oGraphic = System.Drawing.Graphics.FromImage(oThumbNail);

            oGraphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            oGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            oGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            System.Drawing.Rectangle oRectangle = new System.Drawing.Rectangle(0, 0, resizedWidth, resizedHeight);
            oGraphic.DrawImage(imageSource, oRectangle);
            return oThumbNail;
        }
        */

        
    }
}
